package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.FoodDao;
import entity.Food;

public class FoodDaoImpl extends BaseDaoImpl<Food> implements FoodDao {

	@Override
	public List<Food> getNewFoodsTop3() {
		String sql = "SELECT * FROM foods ORDER BY id DESC LIMIT 3";
		return showAll(sql);
	}
	
	@Override
	public Food getDetailsMeun(int id) {
		String sql = "SELECT * FROM foods WHERE id = ?";
		return showAll(sql,id).get(0);
	}
	
	@Override
	public List<Food> listByRs(ResultSet rs) {
		List<Food> list =new ArrayList<Food>();
		try {
			while(rs.next()){
				list.add(new Food(rs.getInt("id"),
						rs.getString("chinesename"),
						rs.getString("englishname"),
						rs.getInt("categoryid"),//cd.getCateById(rs.getInt("categoryid"));
						rs.getString("description"),
						rs.getString("imagefile")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Food> getAllFoods(int pageNum,int pageSize) {
		String sql = "SELECT * FROM foods ORDER BY id DESC limit ?,?";
		int p1=(pageNum-1)*pageSize;
		int p2=pageSize;
		return showAll(sql,p1,p2);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Food> getFoodsListByCategoryId(int pageNum,int pageSize,int categoryid,String search) {
		StringBuffer sql=new StringBuffer();
		
		@SuppressWarnings("rawtypes")
		List parms = new ArrayList();
		if(categoryid==-1 && search!=null && !search.equals("")){
			sql.append("SELECT * FROM foods where ChineseName like ? ");//只搜索
			parms.add("%"+search+"%");
		}else if(categoryid!=-1 && (search==null || search.equals(""))){
			sql.append("SELECT * FROM foods where categoryid=? ");//只分类
			parms.add(categoryid);
		}else if(categoryid!=-1 && search!=null && !search.equals("")){
			sql.append("SELECT * FROM foods where ChineseName like ? and categoryid=? ");//分类+搜索
			parms.add("%"+search+"%");
			parms.add(categoryid);
			
		}else{
			sql.append("SELECT * FROM foods");//不分类+不搜索
		}
		int p1=(pageNum-1)*pageSize;
		int p2=pageSize;
		parms.add(p1);
		parms.add(p2);
		sql.append(" ORDER BY id DESC limit ? , ? ");


		return showAll(sql.toString(),parms.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Food> getFoodsListByCategoryIdAndSearch(int categoryid, String search) {
		StringBuffer sql=new StringBuffer();
		
		@SuppressWarnings("rawtypes")
		List parms = new ArrayList();
		if(categoryid==-1 && search!=null && !search.equals("")){
			sql.append("SELECT * FROM foods where ChineseName like ? ");//只搜索
			parms.add("%"+search+"%");
		}else if(categoryid!=-1 && (search==null || search.equals(""))){
			sql.append("SELECT * FROM foods where categoryid=? ");//只分类
			parms.add(categoryid);
		}else if(categoryid!=-1 && search!=null && !search.equals("")){
			sql.append("SELECT * FROM foods where ChineseName like ? and categoryid=? ");//分类+搜索
			parms.add("%"+search+"%");
			parms.add(categoryid);
			
		}else{
			sql.append("SELECT * FROM foods");//不分类+不搜索
		}
		return showAll(sql.toString(), parms.toArray());
	}

}
